"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.columnRender = columnRender;
exports.renderColumnsTitle = exports.defaultOnFilter = void 0;

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _get = _interopRequireDefault(require("rc-util/lib/utils/get"));

var _react = _interopRequireDefault(require("react"));

var _ = require(".");

var _cellRenderToFromItem = _interopRequireDefault(require("./cellRenderToFromItem"));

var _excluded = ["children"],
    _excluded2 = ["children"];

/**
 * 增加了 icon 的功能 render title
 *
 * @param item
 */
var renderColumnsTitle = function renderColumnsTitle(item) {
  var title = item.title;

  if (title && typeof title === 'function') {
    return title(item, 'table', (0, _jsxRuntime.jsx)(_proUtils.LabelIconTip, {
      label: null,
      tooltip: item.tooltip || item.tip
    }));
  }

  return (0, _jsxRuntime.jsx)(_proUtils.LabelIconTip, {
    label: title,
    tooltip: item.tooltip || item.tip,
    ellipsis: item.ellipsis
  });
};
/** 判断可不可编辑 */


exports.renderColumnsTitle = renderColumnsTitle;

function isEditableCell(text, rowData, index, editable) {
  if (typeof editable === 'boolean') {
    return editable === false;
  }

  return (editable === null || editable === void 0 ? void 0 : editable(text, rowData, index)) === false;
}
/**
 * 默认的 filter 方法
 *
 * @param value
 * @param record
 * @param dataIndex
 * @returns
 */


var defaultOnFilter = function defaultOnFilter(value, record, dataIndex) {
  var recordElement = Array.isArray(dataIndex) ? (0, _get.default)(record, dataIndex) : record[dataIndex];
  var itemValue = String(recordElement);
  return String(itemValue) === String(value);
};

exports.defaultOnFilter = defaultOnFilter;

var OptionsCell = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2.default)(OptionsCell, _React$Component);

  var _super = (0, _createSuper2.default)(OptionsCell);

  function OptionsCell() {
    (0, _classCallCheck2.default)(this, OptionsCell);
    return _super.apply(this, arguments);
  }

  (0, _createClass2.default)(OptionsCell, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      var _this$props = this.props,
          children = _this$props.children,
          restProps = (0, _objectWithoutProperties2.default)(_this$props, _excluded);
      var nextChildren = nextProps.children,
          restNextProps = (0, _objectWithoutProperties2.default)(nextProps, _excluded2);
      return !(0, _proUtils.isDeepEqualReact)(restProps, restNextProps);
    }
  }, {
    key: "render",
    value: function render() {
      return (0, _jsxRuntime.jsx)(_antdV.Space, {
        children: this.props.children(this.props.form)
      });
    }
  }]);
  return OptionsCell;
}(_react.default.Component);
/**
 * 这个组件负责单元格的具体渲染
 *
 * @param param0
 */


function columnRender(_ref) {
  var columnProps = _ref.columnProps,
      text = _ref.text,
      rowData = _ref.rowData,
      index = _ref.index,
      columnEmptyText = _ref.columnEmptyText,
      counter = _ref.counter,
      type = _ref.type,
      subName = _ref.subName,
      editableUtils = _ref.editableUtils;
  var action = counter.action,
      prefixName = counter.prefixName,
      editableForm = counter.editableForm;

  var _editableUtils$isEdit = editableUtils.isEditable((0, _objectSpread2.default)((0, _objectSpread2.default)({}, rowData), {}, {
    index: index
  })),
      isEditable = _editableUtils$isEdit.isEditable,
      recordKey = _editableUtils$isEdit.recordKey;

  var _columnProps$renderTe = columnProps.renderText,
      renderText = _columnProps$renderTe === void 0 ? function (val) {
    return val;
  } : _columnProps$renderTe;
  var renderTextStr = renderText(text, rowData, index, action);
  var mode = isEditable && !isEditableCell(text, rowData, index, columnProps === null || columnProps === void 0 ? void 0 : columnProps.editable) ? 'edit' : 'read';
  var textDom = (0, _cellRenderToFromItem.default)({
    text: renderTextStr,
    valueType: columnProps.valueType || 'text',
    index: index,
    rowData: rowData,
    subName: subName,
    columnProps: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, columnProps), {}, {
      // 为了兼容性，原来写了个错别字
      // @ts-ignore
      entry: rowData,
      entity: rowData
    }),
    counter: counter,
    columnEmptyText: columnEmptyText,
    type: type,
    recordKey: recordKey,
    mode: mode,
    prefixName: prefixName,
    editableUtils: editableUtils
  });
  var dom = mode === 'edit' ? textDom : (0, _proUtils.genCopyable)(textDom, columnProps, renderTextStr);
  /** 如果是编辑模式，并且 renderFormItem 存在直接走 renderFormItem */

  if (mode === 'edit') {
    if (columnProps.valueType === 'option') {
      return (0, _jsxRuntime.jsx)(OptionsCell, {
        record: rowData,
        form: editableForm,
        children: function children(inform) {
          return editableUtils.actionRender((0, _objectSpread2.default)((0, _objectSpread2.default)({}, rowData), {}, {
            index: columnProps.index || index
          }), inform);
        }
      });
    }

    return dom;
  }

  if (!columnProps.render) {
    var isReactRenderNode = /*#__PURE__*/_react.default.isValidElement(dom) || ['string', 'number'].includes((0, _typeof2.default)(dom));
    return !(0, _proUtils.isNil)(dom) && isReactRenderNode ? dom : null;
  }

  var renderDom = columnProps.render(dom, rowData, index, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, action), editableUtils), (0, _objectSpread2.default)((0, _objectSpread2.default)({}, columnProps), {}, {
    isEditable: isEditable,
    type: 'table'
  })); // 如果是合并单元格的，直接返回对象

  if ((0, _.isMergeCell)(renderDom)) {
    return renderDom;
  }

  if (renderDom && columnProps.valueType === 'option' && Array.isArray(renderDom)) {
    return (0, _jsxRuntime.jsx)(_antdV.Space, {
      size: 16,
      children: renderDom
    });
  }

  return renderDom;
}